package com.cyloer.smartThread.ThreadExecutors;

import java.util.concurrent.*;

/**
 * @Description:
 * @author: xiangliu5
 * @date: 2021/5/7 13:58
 */
public class TestConcurrentLinkedQueue {

    private static final   int  num=10;
    private static final ConcurrentLinkedQueue<String> concurrentLinkedQueue=new ConcurrentLinkedQueue<String>();
    private static final ExecutorService executorService =new ThreadPoolExecutor (4,10,20, TimeUnit.SECONDS,new  LinkedBlockingQueue<Runnable>());
    private static final CountDownLatch downLatch=new CountDownLatch(num);


    public static void test(){
        for(int i=0;i<num;i++){
            concurrentLinkedQueue.offer(String.format("消费者{%s}",i));

        }
        for(int a=0;a<num;a++) {
            executorService.submit(new Deviced(concurrentLinkedQueue, String.format("线程{%s}", a)));
        }
        try {
            downLatch.await();
            executorService.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    public static void main(String[] args) {
        test();
    }



    public static  class Deviced implements Runnable{

        private ConcurrentLinkedQueue<String> concurrentLinkedQueue;
        private String name;
        private CountDownLatch downLatch;


        public Deviced(ConcurrentLinkedQueue<String> concurrentLinkedQueue, String name) {
            this.concurrentLinkedQueue = concurrentLinkedQueue;
            this.name = name;
        }

        public ConcurrentLinkedQueue<String> getConcurrentLinkedQueue() {
            return concurrentLinkedQueue;
        }

        public void setConcurrentLinkedQueue(ConcurrentLinkedQueue<String> concurrentLinkedQueue) {
            this.concurrentLinkedQueue = concurrentLinkedQueue;
        }

        public String getName() {
            return name;
        }

        public  void setName(String name) {
            this.name = name;
        }


        @Override
        public  void run() {
            while (!concurrentLinkedQueue.isEmpty())
            {
                concurrentLinkedQueue.poll();
                System.out.println(String.format("执行者{%s},前面还有{%s}",name,concurrentLinkedQueue.size()));
            }
        }
    }
}

